package com.yunsc.admin.controller.store;


import cn.dev33.satoken.stp.StpUtil;
import com.yunsc.common.base.BaseConstant;
import com.yunsc.common.utils.Result;
import com.yunsc.system.dto.AuthParamsDto;
import com.yunsc.store.service.YunscLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-04-11 14:36
 */
@Api(value = "后台管理登入接口", tags = "商家信息接口")
@Slf4j
@RestController
// @RequestMapping("store")
public class StoreLoginController {

    @Resource
    private YunscLoginService loginService;

    @ApiOperation(value = "登录", notes = "登录参数: username, password", httpMethod = "POST")
    @PostMapping(value = "/passport/login")
    public Result<?> storeLogin(@Validated @RequestBody AuthParamsDto authParam) {
        Map<String, Object> map = loginService.storeLogin(authParam);
        return Result.ok("登录成功", map);
    }


    @ApiOperation(value = "登出", notes = "登出", httpMethod = "POST")
    @PostMapping(value = "/passport/logout")
    public Result storeLogOut(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader(BaseConstant.tokenHeader);
        //根据token退出登录
        StpUtil.logoutByTokenValue(token);
        return Result.ok("退出登录成功");
    }
}
